﻿package frEngine.postProcess
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import flash.display3D.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.postProcess.postEffect.*;
    import frEngine.render.layer.*;

    public class PostProcess extends Object
    {
        public var firstBuffer:PostTextureBuffer;
        public var enableWriteToTexture:Boolean = true;
        public var uvValue:Vector.<Number>;
        private var _scene:Scene3D;
        public var clipRect:Rectangle;
        private var _postEffects:Dictionary;
        private var _curPE:PostEffectBase;
        public var renderLayerList:RenderList;
        private var _nonePostEffect:PostEffectBase;
        private var _spaceWarp:PeSpaceWarp;
        public var JXmohu:PeJXmohu;
        private var _deathGrey:PeDeathGrey;
        public var enabledShadow:Boolean = true;
        public var isRenderingShadow:Boolean = false;
        public var forceNotToTexture:Boolean = false;
        public static const instance:PostProcess = new PostProcess;

        public function PostProcess()
        {
            this.uvValue = this.Vector.<Number>([1, 1, 0, 0]);
            this.clipRect = new Rectangle();
            this._postEffects = new Dictionary();
            this.renderLayerList = new RenderList();
            return;
        }// end function

        public function init(param1:Scene3D) : void
        {
            this._scene = param1;
            this.firstBuffer = new PostTextureBuffer(Scene3D.textureWidth, Scene3D.textureHeight);
            this._nonePostEffect = new PeNonePostEffect(this);
            this._postEffects[this._nonePostEffect.type] = this._nonePostEffect;
            this.JXmohu = new PeJXmohu(this);
            this.JXmohu.ResetInputAndOutput(this.firstBuffer, null);
            this._postEffects[this.JXmohu.type] = this.JXmohu;
            this._deathGrey = new PeDeathGrey(this);
            this._postEffects[EPostEffectType.PeDeathGrey] = this._deathGrey;
            this._deathGrey.ResetInputAndOutput(this.firstBuffer, null);
            this._spaceWarp = new PeSpaceWarp(this);
            this._spaceWarp.ResetInputAndOutput(this.firstBuffer, null);
            this._postEffects[this._spaceWarp.type] = this._spaceWarp;
            this._curPE = this._spaceWarp;
            return;
        }// end function

        public function SetActivePostEffect(param1:int) : void
        {
            var _loc_2:* = this._postEffects[param1];
            if (_loc_2)
            {
                this._curPE = _loc_2;
                this._curPE.ResetInputAndOutput(this.firstBuffer, null);
            }
            return;
        }// end function

        public function get needRenderToTexture() : Boolean
        {
            if (this.forceNotToTexture)
            {
                return false;
            }
            if (this._curPE == this._deathGrey || this._curPE == this.JXmohu)
            {
                return true;
            }
            if (!this.enableWriteToTexture || this._curPE.renderListLen == 0 || !this._curPE.hasUpload)
            {
                return false;
            }
            return true;
        }// end function

        public function doPostRendering(param1:Context3D) : void
        {
            this._curPE.draw(param1);
            return;
        }// end function

        public function resize(param1:Rectangle) : void
        {
            var _loc_2:PostEffectBase = null;
            for each (_loc_2 in this._postEffects)
            {
                
                _loc_2.resize(param1);
            }
            this.viewPortResize(param1);
            return;
        }// end function

        public function immediatelyToUpload(param1:Scene3D) : void
        {
            this.firstBuffer.immediatelyUploadTexture(param1);
            this._spaceWarp.immediatelyToUpload(param1);
            this.JXmohu.immediatelyToUpload(param1);
            this._deathGrey.immediatelyToUpload(param1);
            return;
        }// end function

        public function reUpload(param1:Scene3D) : void
        {
            this.firstBuffer.reUpload(param1);
            this._spaceWarp.reUpload(param1);
            this.JXmohu.reUpload(param1);
            this._deathGrey.reUpload(param1);
            return;
        }// end function

        public function enableBlueFilter(param1:Boolean) : void
        {
            if (param1)
            {
                this.SetActivePostEffect(EPostEffectType.PeJXmohu);
            }
            else
            {
                this.SetActivePostEffect(EPostEffectType.PeSpaceWarp);
            }
            return;
        }// end function

        public function enableDeathGreyFilter(param1:Boolean) : void
        {
            if (param1)
            {
                this.SetActivePostEffect(EPostEffectType.PeDeathGrey);
            }
            else
            {
                this.SetActivePostEffect(EPostEffectType.PeSpaceWarp);
            }
            return;
        }// end function

        private function viewPortResize(param1:Rectangle) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            if (param1)
            {
                _loc_6 = param1.width;
                _loc_7 = param1.height;
                var _loc_8:* = _loc_6 / Scene3D.textureWidth;
                _loc_2 = _loc_6 / Scene3D.textureWidth;
                this.uvValue[0] = _loc_8;
                var _loc_8:* = _loc_7 / Scene3D.textureHeight;
                _loc_3 = _loc_7 / Scene3D.textureHeight;
                this.uvValue[1] = _loc_8;
                var _loc_8:* = (1 - _loc_2) * 0.5;
                _loc_4 = (1 - _loc_2) * 0.5;
                this.uvValue[2] = _loc_8;
                var _loc_8:* = (1 - _loc_3) * 0.5;
                _loc_5 = (1 - _loc_3) * 0.5;
                this.uvValue[3] = _loc_8;
                this.clipRect.width = _loc_6;
                this.clipRect.height = _loc_7;
                this.clipRect.x = _loc_4 * Scene3D.textureWidth;
                this.clipRect.y = _loc_5 * Scene3D.textureHeight;
            }
            return;
        }// end function

        public function render(param1:RenderList, param2:Context3D) : uint
        {
            var _loc_7:Layer3DSort = null;
            var _loc_8:Vector.<Mesh3D> = null;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            param2.setStencilReferenceValue(1);
            param2.setStencilActions(Context3DTriangleFace.FRONT_AND_BACK, Context3DCompareMode.ALWAYS, Context3DStencilAction.KEEP);
            var _loc_3:* = param1.layers;
            var _loc_4:* = _loc_3.length;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_4)
            {
                
                _loc_7 = _loc_3[_loc_6];
                if (_loc_7.layerId == Layer3DManager.warpLayer)
                {
                }
                else
                {
                    if (_loc_7.layerId == Layer3DManager.backGroudMaskLayer)
                    {
                        param2.setColorMask(false, false, false, true);
                    }
                    if (_loc_7.layerId == Layer3DManager.PixsMaskLayer)
                    {
                        this.renderPixsMaskLayer(param2, param1);
                        param2.setStencilReferenceValue(1);
                        param2.setStencilActions(Context3DTriangleFace.FRONT_AND_BACK, Context3DCompareMode.GREATER_EQUAL, Context3DStencilAction.KEEP);
                    }
                    else
                    {
                        if (_loc_7.isActive)
                        {
                            _loc_7.sort();
                        }
                        _loc_8 = _loc_7.list;
                        _loc_9 = _loc_8.length;
                        if (_loc_9 > 0)
                        {
                            _loc_10 = 0;
                            while (_loc_10 < _loc_9)
                            {
                                
                                _loc_8[_loc_10].draw(false);
                                _loc_5++;
                                _loc_10++;
                            }
                        }
                        if (_loc_7.layerId == Layer3DManager.backGroudMaskLayer)
                        {
                            param2.setColorMask(true, true, true, true);
                        }
                    }
                }
                _loc_6++;
            }
            return _loc_5;
        }// end function

        private function renderPixsMaskLayer(param1:Context3D, param2:RenderList) : void
        {
            var _loc_6:int = 0;
            param1.setStencilActions(Context3DTriangleFace.FRONT_AND_BACK, Context3DCompareMode.ALWAYS, Context3DStencilAction.SET);
            var _loc_3:* = param2.layMap[Layer3DManager.PixsMaskLayer];
            if (!_loc_3)
            {
                return;
            }
            if (_loc_3.isActive)
            {
                _loc_3.sort();
            }
            var _loc_4:* = _loc_3.list;
            var _loc_5:* = _loc_4.length;
            if (_loc_5 > 0)
            {
                _loc_6 = 0;
                while (_loc_6 < _loc_5)
                {
                    
                    _loc_4[_loc_6].draw(false);
                    _loc_6++;
                }
            }
            return;
        }// end function

        public function renderShadow(param1:Context3D, param2:RenderList) : void
        {
            var _loc_6:Layer3DSort = null;
            var _loc_7:Vector.<Mesh3D> = null;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:Mesh3D = null;
            if (!this.enabledShadow)
            {
                return;
            }
            this.isRenderingShadow = true;
            this.renderPixsMaskLayer(param1, param2);
            this.isRenderingShadow = false;
            param1.setStencilActions(Context3DTriangleFace.FRONT_AND_BACK, Context3DCompareMode.LESS_EQUAL, Context3DStencilAction.ZERO);
            var _loc_3:* = param2.layers;
            var _loc_4:* = _loc_3.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_4)
            {
                
                _loc_6 = _loc_3[_loc_5];
                if (_loc_6.layerId == Layer3DManager.warpLayer || _loc_6.layerId == Layer3DManager.PixsMaskLayer)
                {
                }
                else
                {
                    if (_loc_6.isActive)
                    {
                        _loc_6.sort();
                    }
                    _loc_7 = _loc_6.list;
                    _loc_8 = _loc_7.length;
                    if (_loc_8 > 0)
                    {
                        _loc_9 = 0;
                        while (_loc_9 < _loc_8)
                        {
                            
                            _loc_10 = _loc_7[_loc_9];
                            _loc_10.drawShadow();
                            _loc_9++;
                        }
                    }
                }
                _loc_5++;
            }
            param1.setStencilActions(Context3DTriangleFace.FRONT_AND_BACK, Context3DCompareMode.ALWAYS, Context3DStencilAction.KEEP);
            return;
        }// end function

        public function renderDepth(param1:RenderList) : void
        {
            var _loc_5:Layer3DSort = null;
            var _loc_6:Vector.<Mesh3D> = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:Mesh3D = null;
            var _loc_2:* = param1.layers;
            var _loc_3:* = _loc_2.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = _loc_2[_loc_4];
                if (_loc_5.layerId == Layer3DManager.warpLayer || _loc_5.layerId == Layer3DManager.backGroudMaskLayer)
                {
                }
                else
                {
                    _loc_6 = _loc_5.list;
                    _loc_7 = _loc_6.length;
                    if (_loc_7 > 0)
                    {
                        _loc_8 = 0;
                        while (_loc_8 < _loc_7)
                        {
                            
                            _loc_9 = _loc_6[_loc_8];
                            if (_loc_9.material.materialParams.depthWrite == true)
                            {
                                _loc_9.render.drawDepth(_loc_9, 0.3, 0.3, 0.3, 1);
                            }
                            _loc_8++;
                        }
                    }
                }
                _loc_4++;
            }
            return;
        }// end function

    }
}
